package ui.panels;

import entities.Patient;
import entities.PatientMaterial;
import entities.User;
import java.awt.Color;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JOptionPane;
import ui.windows.ChoseDoctorDialog;
import ui.windows.RemedySchedulerFrame;

public class CreatePMaterialPanel extends javax.swing.JPanel {

    /** Creates new form CreatePMaterialPanel */
    public CreatePMaterialPanel(RemedySchedulerFrame mainFrame, PMaterialManagementPanel parentPanel) {
        initComponents();
        this.mainFrame = mainFrame;
        this.parentPanel = parentPanel;
        includeModel();
    }

    private void includeModel() {
        checkPID = false;
        checkDID = false;
        checkDate = false;
        lbCheckPID.setText("");
        lbCheckDate.setText("");
    }

    public void setSelectedPatient(Patient patient) {
        this.selectedPatient = patient;
        txtPtID.setText(patient.getIdentityCard());
    }

    public void setSelectedDoctor(User doctor) {
        this.selectedDoctor = doctor;
        if (selectedDoctor != null) {
            txtDoctorID.setText(selectedDoctor.getLoginID());
            checkDID = true;
        } else {
            txtDoctorID.setText("");
            checkDID = false;
        }
    }

    private void createRecord() {
        checkID();
        checkDate();
        if (checkPID && checkDID && checkDate) {
            Date startDate = dcStartDate.getDate();
            if (startDate != null) {
                PatientMaterial pm = new PatientMaterial();
                pm.setPatientID(selectedPatient.getPatientID());
                pm.setEmployeeID(selectedDoctor.getUserID());
                pm.setStartDate(dcStartDate.getDate());
                boolean returnVal = parentPanel.createRecord(pm);
                if (returnVal) {
                    close();
                    JOptionPane.showMessageDialog(null, "New record is created !");
                } else {
                    JOptionPane.showMessageDialog(null, "Cannot insert !", "Error", JOptionPane.WARNING_MESSAGE);
                }
            } else {
                lbCheckDate.setText("enter startdate !");
                lbCheckDate.setForeground(Color.red);
            }
        }
    }

    private void checkID() {
        String cid = txtPtID.getText();
        if (cid != null && cid.length() != 0) {
            try {
                int id = Integer.parseInt(cid);
                if (id < 0) {
                    throw new Exception();
                }
                if (cid.length() == 9) {
                    selectedPatient = parentPanel.getPatientByIDC(cid);
                    if (selectedPatient != null) {
                        lbCheckPID.setText("valid");
                        lbCheckPID.setForeground(new Color(102, 102, 102));
                        checkPID = true;
                    }
                } else {
                    lbCheckPID.setText("check...");
                    lbCheckPID.setForeground(Color.blue);
                    checkPID = false;
                }
            } catch (Exception e) {
                lbCheckPID.setText("invalid !");
                lbCheckPID.setForeground(Color.red);
                checkPID = false;
            }
        } else {
            lbCheckPID.setText("enter IDC !");
            lbCheckPID.setForeground(Color.blue);
            checkPID = false;
        }
    }
    
    private void checkDate() {
        Date startDate = getOnlyDate(dcStartDate.getDate());
        if(!startDate.before(getOnlyDate(Calendar.getInstance().getTime()))) {
            checkDate = true;
            lbCheckDate.setText("valid");
            lbCheckDate.setForeground(new Color(102, 102, 102));
        } else {
            checkDate = false;
            lbCheckDate.setText("invalid !");
            lbCheckDate.setForeground(Color.red);
        }
    }
    
    private Date getOnlyDate(Date date) {
        if(date != null) {
            return new Date(date.getYear(), date.getMonth(), date.getDate());
        }
        return null;
    }

    private void choseDoctor() {
        ChoseDoctorDialog dialog = new ChoseDoctorDialog(mainFrame, this);
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
    }

    private void clear() {
        txtPtID.setText("");
        lbCheckPID.setText("");
        lbChoise.setText("choise...");
        lbChoise.setForeground(Color.blue);
        txtDoctorID.setText("");
        dcStartDate.setDate(null);        
        checkPID = false;
        checkDID = false;
    }

    private void close() {
        clear();
        parentPanel.closeTarget();
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtPtID = new javax.swing.JTextField();
        lbCheckPID = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        dcStartDate = new com.toedter.calendar.JDateChooser();
        lbChoise = new javax.swing.JLabel();
        lbCheckDate = new javax.swing.JLabel();
        txtDoctorID = new javax.swing.JTextField();
        lbClose = new javax.swing.JLabel();
        bCreate = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Patient's Material", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Agency FB", 1, 14))); // NOI18N

        jLabel2.setText("IdentityCard:");

        txtPtID.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtPtIDCaretUpdate(evt);
            }
        });

        lbCheckPID.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbCheckPID.setText("value");

        jLabel4.setText("DoctorID:");

        jLabel5.setText("StartDate:");

        lbChoise.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbChoise.setForeground(new java.awt.Color(0, 0, 255));
        lbChoise.setText("choise...");
        lbChoise.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lbChoiseMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lbChoiseMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lbChoiseMouseExited(evt);
            }
        });

        lbCheckDate.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbCheckDate.setText("value");

        txtDoctorID.setEditable(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel2)
                    .addComponent(jLabel5))
                .addGap(37, 37, 37)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txtPtID, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dcStartDate, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE)
                    .addComponent(txtDoctorID, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lbChoise)
                    .addComponent(lbCheckPID, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                    .addComponent(lbCheckDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(54, 54, 54))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPtID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbCheckPID)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtDoctorID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbChoise))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5)
                    .addComponent(dcStartDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbCheckDate))
                .addContainerGap(69, Short.MAX_VALUE))
        );

        lbClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ui/images/close_20.png"))); // NOI18N
        lbClose.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lbCloseMouseClicked(evt);
            }
        });

        bCreate.setText("Create");
        bCreate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCreateActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(601, Short.MAX_VALUE)
                .addComponent(lbClose)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(bCreate, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(62, 62, 62)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(132, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbClose)
                .addGap(27, 27, 27)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bCreate)
                .addContainerGap(98, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void txtPtIDCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtPtIDCaretUpdate
    checkID();
}//GEN-LAST:event_txtPtIDCaretUpdate

private void lbChoiseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lbChoiseMouseClicked
    choseDoctor();
}//GEN-LAST:event_lbChoiseMouseClicked

private void lbChoiseMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lbChoiseMouseEntered
    lbChoise.setForeground(Color.red);
}//GEN-LAST:event_lbChoiseMouseEntered

private void lbChoiseMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lbChoiseMouseExited
    lbChoise.setForeground(Color.blue);
}//GEN-LAST:event_lbChoiseMouseExited

private void bCreateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCreateActionPerformed
    createRecord();
}//GEN-LAST:event_bCreateActionPerformed

private void lbCloseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lbCloseMouseClicked
    close();
}//GEN-LAST:event_lbCloseMouseClicked
    private RemedySchedulerFrame mainFrame;
    private PMaterialManagementPanel parentPanel;
    private Patient selectedPatient;
    private User selectedDoctor;
    private boolean checkPID;
    private boolean checkDID;
    private boolean checkDate;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bCreate;
    private com.toedter.calendar.JDateChooser dcStartDate;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lbCheckDate;
    private javax.swing.JLabel lbCheckPID;
    private javax.swing.JLabel lbChoise;
    private javax.swing.JLabel lbClose;
    private javax.swing.JTextField txtDoctorID;
    private javax.swing.JTextField txtPtID;
    // End of variables declaration//GEN-END:variables
}
